import { db } from "./cloudInit.js";
import { auditXinCards, UserResult, ManageFeedback } from "./manager/querys.js";
// 心语签相关的数据存储
export default {
    state: () => ({
        auditXinCards,
        index: 0,
        UserResult,
        ManageFeedback,
    }),
    getters: {
        AuditTotal(state) {
            return {
                audit: state.auditXinCards.total,
            };
        },
    },
    mutations: {
        updateFeedback(state, feedback) {
            const id = feedback._id;
            delete feedback._id;
            delete feedback._openid;
            return db.collection("Feedback").doc(id).update({
                data: feedback,
            });
        },
        filterAuditXinCards(state, confirmXinDocs) {
            confirmXinDocs.forEach((i) => {
                const index = state.auditXinCards.findIndex(
                    (card) => card._id === i
                );
                state.auditXinCards.splice(index, 1);
            });
        },
        searchUserResult(state, result) {
            state.UserResult = result;
        },
        UserManagerChange(state, { _id, isManager }) {
            state.UserResult.some((item) => {
                if (item._id === _id) {
                    item.isManager = isManager;
                    return true;
                }
            });
        },
    },
    actions: {
        confirmPublic({ commit }, confirmXinDocs) {
            return wx.cloud
                .callFunction({
                    name: "AuditXin",
                    data: {
                        confirmXinDocs,
                    },
                })
                .then((res) => {
                    commit("filterAuditXinCards", confirmXinDocs);
                    console.log("审核完成", res);
                });
        },
        ApplyManager({ commit }, { UserDoc, isManager }) {
            return wx.cloud
                .callFunction({
                    name: "ApplyManager",
                    data: {
                        doc: UserDoc,
                        isManager,
                    },
                })
                .then(({ result }) => {
                    console.log("添加管理员成功", result.data);
                    commit("UserManagerChange", {
                        _id: UserDoc,
                        isManager,
                    });
                });
        },
    },
};
